package com.cw.frame.codegen.parameter;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.cw.frame.codegen.Configuration;

/**
 * Factory to create parameter context by the database table schema.
 *
 * @author chenw
 */
public class ParamContextFactory4PropertiesImpl extends AbstractParamContextFactoryImpl {

    @Override
    protected void fulfillContextOnDemand(ParamContext context, Configuration configuration) throws Exception {
        String packageNames = configuration.getPackages();//包名集合
        String tableNames = configuration.getTableNames();//表名集合
        String tableCnNames = configuration.getTableCnNames();//表中文名集合
        String pIds = configuration.getPkIds();//主键id集合
        String pkIdDataTypes = configuration.getPkIdDataTypes();//主键id数据类型集合
        String author = configuration.getAuthor();
        String pagePath = configuration.getPagePath();
        String moduleId = configuration.getModuleId();
        String moduleIdCn = configuration.getModuleIdCn();
        if (StringUtils.isEmpty(packageNames) || StringUtils.isEmpty(tableNames)) {
            return;
        }

        String[] packageNameArray = packageNames.split(",");
        int count = packageNameArray.length;
        String[] tableNamesArray = tableNames.split(",");
        String[] tableCnNamesArray = tableCnNames.split(",");
        String[] pIdsArray = pIds.split(",");
        String[] pkIdDataTypesArray = pkIdDataTypes.split(",");

        if (tableNamesArray.length != count) {
            logger.error("Table names is not with the same size as the packages, please check the conf.properties");
            return;
        }

        List<ParamBundle> paramBundles = new ArrayList<ParamBundle>();
        DatabaseSchemaReader databaseSchemaReader = new DatabaseSchemaReader();
        for (int i = 0; i < count; i++) {
            ParamBundle paramBundle = new ParamBundle();
            paramBundle.setModuleId(moduleId);//模块id
            paramBundle.setModuleIdCn(moduleIdCn);
            String tableName = tableNamesArray[i];
            paramBundle.setPackageName(packageNameArray[i]);
            paramBundle.setPkId(pIdsArray[i]);
            paramBundle.setPkIdDataType(pkIdDataTypesArray[i]);
            String pkIdClass = ParamHelper.getFieldName(pIdsArray[i]);
            paramBundle.setPkIdClass(pkIdClass);
            paramBundle.setPkIdUp(pkIdClass.replaceFirst(pkIdClass.substring(0, 1), pkIdClass.substring(0, 1)
                .toUpperCase()));
            paramBundle.setTableCnName(tableCnNamesArray[i]);
            paramBundle.setTableName(tableName.toLowerCase());
            String tempModuleName = paramBundle.getTableName();
            String moduleName = ParamHelper.getFieldName(tempModuleName);
            paramBundle.setModuleName(moduleName);
            paramBundle.setClassName(moduleName.replaceFirst(moduleName.substring(0, 1), moduleName.substring(0, 1)
                .toUpperCase()));
            paramBundle.setFieldList(databaseSchemaReader.parseTableForMysql(tableName));

            paramBundle.setAuthor(author);

            paramBundle.setPagePath(pagePath);

            paramBundles.add(paramBundle);
        }
        context.setParamBundles(paramBundles);

        // Close the connection
        databaseSchemaReader.closeDBConnection();
    }

}
